package model
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;

	public class Item extends Sprite
	{
		private var imageURL:String;
		private var count:uint=0;
		private var fromItem:Item;
		private var toItem:Item;
		private var info:TextField;
		
		private var loader:Loader=new Loader();
		private var image:Bitmap;
		
		public function Item(imageURL:String)
		{
			this.name=int(Math.random()*100)+this.imageURL;
			this.imageURL=imageURL;
			setItem(imageURL);			
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
		}
		
		
		
		/**
		 * 加载物品图片
		 */
		private function setItem(imageURL:String):void{
			var url:URLRequest=new URLRequest(imageURL);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onloaded);
			loader.load(url);
		}
		
		/**
		 * 加载完后添加到舞台现实
		 */
		private function onloaded(e:Event):void{
			var image:Bitmap;image=Bitmap(loader.content);
			this.addChild(image);	
		}
		
		/**
		 * 鼠标点下
		 */
		private function mouseDownHandler(e:MouseEvent):void{
			
			var copyItem:Item;
			fromItem=e.currentTarget as Item;
			trace((e.currentTarget as Item).name);
			copyItem=fromItem;
			copyItem.x = copyItem.parent.mouseX - copyItem.width/2;
			copyItem.y = copyItem.parent.mouseY - copyItem.height/2;
			//copyItem.startDrag();
			copyItem.parent.addChild(copyItem);
			
			fromItem.x=100;
			fromItem.y=100;
		}
		/**
		 * 鼠标放开
		 */
		private function mouseUpHandler(e:MouseEvent):void{
			var toItem:Item=e.currentTarget as Item;
			trace((e.currentTarget as Item).name);
			
			this.stopDrag();
		}
		
		public function getUrl():String{
			return imageURL;
		}
		
	}
}